﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using System.Web.Script.Services;
using Newtonsoft.Json;
using System.Configuration;
using System.Data;

namespace PromotionEngine
{
    public partial class PropertySelectedList : System.Web.UI.Page
    {
        public class Response
        {
            public List<Model.CustomerProperty> Data { get; set; }
            public int Count { get; set; }
            public string Errors { get; set; }

            public Response(List<Model.CustomerProperty> data, int count)
            {
                this.Data = data;
                this.Count = count;
            }

            public Response(string errors)
            {
                this.Errors = errors;
            }

            public Response()
            {

            }
        }
        public class ResponseDetail
        {
            public List<Model.CustomerPropertyDetail> Data { get; set; }
            public int Count { get; set; }
            public string Errors { get; set; }

            public ResponseDetail(List<Model.CustomerPropertyDetail> data, int count)
            {
                this.Data = data;
                this.Count = count;
            }

            public ResponseDetail(string errors)
            {
                this.Errors = errors;
            }

            public ResponseDetail()
            {

            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            this.hdnPropertyType.Value = Request.QueryString["PropertyType"];
        }
        [WebMethod]
        [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
        public static Response GetPropertys(string strPropertyType)
        {
            List<Model.CustomerProperty> lstCustomerProperty = new List<Model.CustomerProperty>();
            Promotion pmt = new Promotion();
            DataSet dsPromotion = new DataSet();
            if (strPropertyType.ToUpper() == "ITEM")
            {
                dsPromotion = pmt.GetAllItemProperty(ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), HttpContext.Current.Session["CompanyCode"].ToString(),
                                                     ConfigurationManager.AppSettings["PropertyValueType"].ToString(), bool.Parse(HttpContext.Current.Session["IsAdmin"].ToString()));
            }
            else if (strPropertyType.ToUpper() == "CUSTOMER")
            {
                dsPromotion = pmt.GetAllCustomerProperty(ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), HttpContext.Current.Session["CompanyCode"].ToString(),
                                                          ConfigurationManager.AppSettings["PropertyValueType"].ToString(), bool.Parse(HttpContext.Current.Session["IsAdmin"].ToString()));
            }


            if (dsPromotion != null)
            {
                foreach (DataRow r in dsPromotion.Tables[0].Rows)
                {
                    Model.CustomerProperty customerProperty = new Model.CustomerProperty();
                    customerProperty.CompanyCode = r["CompanyCode"].ToString();
                    customerProperty.CompanyName = r["CompanyName"].ToString();
                    customerProperty.PropertyCode = r["PropertyCode"].ToString();
                    customerProperty.PropertyName = r["PropertyName"].ToString();
                    customerProperty.PropertyID = r["PropertyID"].ToString();
                    customerProperty.CreatedUserID = r["CreatedUserID"].ToString();
                    customerProperty.PropertyValueType = r["PropertyValueType"].ToString();
                    customerProperty.ReferenceName = r["ReferenceName"].ToString();
                    lstCustomerProperty.Add(customerProperty);
                }
                var jsonData = new { total = dsPromotion.Tables[0].Rows.Count, lstCustomerProperty = lstCustomerProperty };
                return new Response(lstCustomerProperty, dsPromotion.Tables[0].Rows.Count);
            }
            return new Response();
        }
        [WebMethod]
        [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
        public static ResponseDetail GetPropertyDetail(string propertyID, string strPropertyType)
        {
            List<Model.CustomerPropertyDetail> lstCustomerProperty = new List<Model.CustomerPropertyDetail>();
            Promotion pmt = new Promotion();
            DataSet dsPromotion = new DataSet();
            if (strPropertyType.ToUpper() == "ITEM")
            {
                dsPromotion = pmt.GetAllItemPropertyDetailByPropertyID(ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), propertyID);
            }
            else if (strPropertyType.ToUpper() == "CUSTOMER")
            {
                dsPromotion = pmt.GetAllCustomerPropertyDetailByPropertyID(ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), propertyID);
            }
            if (dsPromotion != null)
            {
                foreach (DataRow r in dsPromotion.Tables[0].Rows)
                {
                    Model.CustomerPropertyDetail customerProperty = new Model.CustomerPropertyDetail();
                    customerProperty.PropertyDetailCode = r["PropertyDetailCode"].ToString();
                    customerProperty.PropertyDetailName = r["PropertyDetailName"].ToString();
                    customerProperty.PropertyDetailID = r["PropertyDetailID"].ToString();
                    customerProperty.PropertyID = r["PropertyID"].ToString();
                    customerProperty.CreatedUserID = r["CreatedUserID"].ToString();

                    lstCustomerProperty.Add(customerProperty);
                }
                var jsonData = new { total = dsPromotion.Tables[0].Rows.Count, lstCustomerProperty = lstCustomerProperty };
                return new ResponseDetail(lstCustomerProperty, dsPromotion.Tables[0].Rows.Count);
            }
            return new ResponseDetail();
        }
    }
}
